package com.tcwlkj.uuService.ui.login;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.tcwlkj.uuService.NetWork.HttpCallBack;
import com.tcwlkj.uuService.NetWork.RetrofitUtils;
import com.tcwlkj.uuService.R;
import com.tcwlkj.uuService.model.CommonResponse;
import com.tcwlkj.uuService.model.VerificationCodeModel;
import com.tcwlkj.uuService.ui.BaseActivity;
import com.tcwlkj.uuService.utils.ChcheUtils;
import com.tcwlkj.uuService.utils.TimeCount;
import com.tcwlkj.uuService.utils.ToastUtil;
import com.tcwlkj.uuService.utils.Tools;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

/**
 * Created by Administrator on 2017/10/9 0009.
 *忘记密码界面
 */

public class ForgetPasswodActivity extends BaseActivity implements TextWatcher {

    @Bind(R.id.edit_phone_resetps)
    EditText edit_phone_resetps;
    @Bind(R.id.edit_ps01_resetps)
    EditText edit_ps01_resetps;
    @Bind(R.id.edit_ps02_resetps)
    EditText edit_ps02_resetps;
    @Bind(R.id.edit_checknum_resetps)
    EditText edit_checknum_resetps;
    @Bind(R.id.btn_submit_resetps)
    Button btn_submit_resetps;
    @Bind(R.id.btn_send_resetps)
    TextView btn_send_resetps;
    @Bind(R.id.iv_new01)
    ImageView iv_new01;
    @Bind(R.id.iv_new02)
    ImageView iv_new02;

    private TimeCount time;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forget_ps);
        ButterKnife.bind(this);
        btn_submit_resetps.setBackgroundResource(R.drawable.bg_btn_gray);
        btn_submit_resetps.setClickable(false);
        initview();
    }

    private void initview() {
        changeTitle("忘记密码");
        time = new TimeCount(baseContext, 60000, 1000, btn_send_resetps);
        edit_phone_resetps.addTextChangedListener(this);
        edit_ps01_resetps.addTextChangedListener(this);
        edit_ps02_resetps.addTextChangedListener(this);
        edit_checknum_resetps.addTextChangedListener(this);

    }
    boolean visible01=true;
    boolean visible02=true;
    @OnClick({R.id.btn_submit_resetps,R.id.btn_send_resetps,R.id.iv_new01,R.id.iv_new02})
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_submit_resetps:
                uppassword();
                break;
            case R.id.btn_send_resetps:
                getCode();
                break;
            case R.id.iv_new01:
                if(visible01){
                    iv_new01.setImageDrawable(getResources().getDrawable(R.mipmap.visible));
                    visible01=false;
                    edit_ps01_resetps.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    iv_new01.setImageDrawable(getResources().getDrawable(R.mipmap.invisible));
                    visible01=true;
                    edit_ps01_resetps.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
            case R.id.iv_new02:
                if(visible02){
                    iv_new02.setImageDrawable(getResources().getDrawable(R.mipmap.visible));
                    visible02=false;
                    edit_ps02_resetps.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }else {
                    iv_new02.setImageDrawable(getResources().getDrawable(R.mipmap.invisible));
                    visible02=true;
                    edit_ps02_resetps.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                break;
        }
    }

    private void uppassword() {
        String  edit_phone=edit_phone_resetps.getText().toString().trim();
        String edit_ps01=edit_ps01_resetps.getText().toString().trim();
        String edit_ps02=edit_ps02_resetps.getText().toString().trim();
        String yzm = edit_checknum_resetps.getText().toString().trim();
        if (!Tools.isMobileNumber(edit_phone)) {
            ToastUtil.getInstance().show("手机号格式不正确");
            return;
        }
        if(!edit_ps01.equals(edit_ps02)){
            ToastUtil.getInstance().show("两次新密码不相同");
            return;
        }
        if (yzm.length() < 4 || yzm.length() > 6) {
            ToastUtil.getInstance().show("验证码格式不正确");
            return;
        }
        if (edit_ps02.length() < 6) {
            ToastUtil.getInstance().show("密码长度不少于6位");
            return;
        }
        if(edit_ps02.length()>20){
            ToastUtil.getInstance().show("密码长度不大于20位");
            return;
        }
        if (Tools.isContainChinese(edit_ps02)) {
            ToastUtil.getInstance().show("密码不能包含汉字,请重新输入");
            return;
        }
        Call<CommonResponse<Object>> httpResultCall = RetrofitUtils.getRequest(this, true)
                .forgetPass("rback",3,yzm,edit_phone,edit_ps02);
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<Object>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<Object> commonResponse= (CommonResponse<Object>) t;
                ToastUtil.getInstance().show(commonResponse.getMessage());
                finish();
            }
            @Override
            public void onFail(int code, String message) {
                ToastUtil.getInstance().show(message);
            }
        });
    }

    public void getCode() {
        String str_phone = edit_phone_resetps.getText().toString().trim();
        if (TextUtils.isEmpty(str_phone)) {
            ToastUtil.getInstance().show("请先输入手机号");
            return;
        } else {
            if (!Tools.isMobileNumber(str_phone)) {
                ToastUtil.getInstance().show("电话号码格式不正确！");
                return;
            }
        }
        Call<CommonResponse<VerificationCodeModel>> httpResultCall = RetrofitUtils.getRequest(this, false).getcode( str_phone, "113,2,3");
        httpResultCall.enqueue(new HttpCallBack<CommonResponse<VerificationCodeModel>>() {
            @Override
            public void onSuccess(Object t) {
                CommonResponse<VerificationCodeModel> commonResponse= (CommonResponse<VerificationCodeModel>) t;
                btn_send_resetps.setClickable(false);
                time.start();
            }
            @Override
            public void onFail(int code, String message) {
                if(code == 40104||code==40102||code==40100){
                    getCode();
                }else {
                    ToastUtil.getInstance().show(message);
                }
            }
        });

    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String phone = edit_phone_resetps.getText().toString().trim();
        String ps01 = edit_ps01_resetps.getText().toString().trim();
        String ps02 = edit_ps02_resetps.getText().toString().trim();
        String yzm = edit_checknum_resetps.getText().toString().trim();
        if (!TextUtils.isEmpty(phone) && !TextUtils.isEmpty(yzm) && !TextUtils.isEmpty(ps01) && !TextUtils.isEmpty(ps02)) {
            btn_submit_resetps.setClickable(true);
            btn_submit_resetps.setBackgroundResource(R.drawable.bg_btn_mainred);
        } else {
            btn_submit_resetps.setClickable(false);
            btn_submit_resetps.setBackgroundResource(R.drawable.bg_btn_gray);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
